package ie.dit.reg08.realiser;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;

public class AttributeWordCount {
	
	private String attribute;
	private HashMap<String, AttributeValueWordCount> valueWordCounts;
	
	public AttributeWordCount(String att){
		
		attribute = att;
		valueWordCounts = new HashMap<String, AttributeValueWordCount>();
	}
	
	public boolean hasAttributeValue(String val){
		
		return valueWordCounts.containsKey(val);
	}

	private void addAttributeValue(String val){
		valueWordCounts.put(val, new AttributeValueWordCount(val));
	}
	
	public void incrementAttributeValuewordCounts(String val, String description){
		
		// Check if the attribute value is on the list and if not add it before we add any word counts to it
		if(!hasAttributeValue(val)){
    		addAttributeValue(val);
    	}
		
		StringTokenizer st = new StringTokenizer(description, " ,'()[];.\n\t", false);
	    while (st.hasMoreTokens()){
	    	String currentWord = st.nextToken();
	    	
	    	if(!isOnStopWordList(currentWord)){
	    		valueWordCounts.get(val).incrementWordCount(currentWord);
	    	}
	    }
	}
	
	private boolean isOnStopWordList(String word){
		
		String stopWordList[] = {"and", "the", "it's", "a", "in", "of", "that"};
		boolean onStopWordList = false;
		for(int i = 0; i < stopWordList.length; i++){
			if(stopWordList[i].equalsIgnoreCase(word)){
				onStopWordList = true;
				break;
			}
		}
		
		return onStopWordList;
	}
	
	public String toString(){
		
		StringBuffer s = new StringBuffer("");
	    
		
	    Set<String> ks = valueWordCounts.keySet();
		Iterator<String> iterKey = ks.iterator();
		String sKey;
		while(iterKey.hasNext()) {
			
			s.append(attribute);
			s.append(": ");
			
			sKey = iterKey.next();
			s.append(sKey);
			s.append(": ");
	    	AttributeValueWordCount valueWordCount = valueWordCounts.get(sKey);
	    	s.append(valueWordCount.toString() + "\n");
		}
		
	    return s.toString();
	}
}
